<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright 2010 Prometheus Consulting
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
    http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!-- This stylesheet does not work yet, still Work-in-progress -->
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
	xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
	xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
	xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
	xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
	xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
	xmlns:xlink="http://www.w3.org/1999/xlink" 
	xmlns:dc="http://purl.org/dc/elements/1.1/" 
	xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
	xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
	xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
	xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
	xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
	xmlns:math="http://www.w3.org/1998/Math/MathML" 
	xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" 
	xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
	xmlns:ooo="http://openoffice.org/2004/office" 
	xmlns:ooow="http://openoffice.org/2004/writer" 
	xmlns:oooc="http://openoffice.org/2004/calc" 
	xmlns:dom="http://www.w3.org/2001/xml-events" 
	xmlns:xforms="http://www.w3.org/2002/xforms" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">
	<xsl:output
		method="xml"
		omit-xml-declaration="no"
		indent="yes"/>
	
	<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">
		type="text/xsl" href="MaduraHTML-1.0.xsl"
	</xsl:processing-instruction> 
<!--
Todo:
Headings are not working at all yet
images not done
Should extract stuff from title information
can we detect landscape?
Can we detect code? It looks like tables
References need thought
What about toc etc?
Need to assume some manual editing at the end, so figure the cut-off point.
-->
	<doc>
	<title>
		<xsl:apply-templates select="//text:user-field-decls"/>
		<Revision>$Revision: 1.3 $</Revision>
		<log><![CDATA[
		
		]]>
		</log>
		<references>
			<reference t="Spring Framework" url="http://www.springframework.org" />
			<reference t="jBPM" url="http://www.jboss.com/products/jbpm" />
			<reference t="slf4j" url="http://www.slf4j.org" />
			<reference t="Apache License" url="http://www.apache.org/" />
			<reference t="Sun Microsystems" url="http://www.sun.com" />
			<reference t="XYZ" /><!-- hard copy reference, no URL -->
		</references>
	</title>
			<xsl:apply-templates />
	</doc>
	</xsl:template>
	
	<xsl:template match="text:user-field-decl[@text:name='DocAuthor']">
			<Author><xsl:value-of select="@office:string-value"/></Author>
	</xsl:template>

	<xsl:template match="text:user-field-decl[@text:name='MainTitle']">
			<MainTitle><xsl:value-of select="@office:string-value"/></MainTitle>
	</xsl:template>

	<xsl:template match="text:user-field-decl[@text:name='SubTitle']">
			<SubTitle><xsl:value-of select="@office:string-value"/></SubTitle>
	</xsl:template>

	<xsl:template match="text:user-field-decls">
			<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="office:document-content">
			<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="text:table-of-content">
	</xsl:template>

	<xsl:template match="office:body">
		<body>
		<xsl:apply-templates/>
		</body>
	</xsl:template>

	<xsl:template match="office:text">
		<xsl:apply-templates/>
		<!-- 
		<xsl:for-each-group select="*" group-starting-with="text:h[@text:outline-level='1']">
      	<h1>
      		<xsl:attribute name="t">
      			<xsl:value-of select="text()"/>
      		</xsl:attribute>
	 	    <xsl:for-each select="text:h[@text:outline-level='2']">
	      	<h2>
	      		<xsl:attribute name="t">
	      			<xsl:value-of select="text()"/>
	      		</xsl:attribute>
		 	    <xsl:for-each select="text:h[@text:outline-level='3']">
		      	<h3>
		      		<xsl:attribute name="t">
		      			<xsl:value-of select="text()"/>
		      		</xsl:attribute>
			 	    <xsl:for-each select="text:h[@text:outline-level='4']">
			      	<h4>
			      		<xsl:attribute name="t">
			      			<xsl:value-of select="text()"/>
			      		</xsl:attribute>
			        </h4>
					</xsl:for-each>
		        </h3>
				</xsl:for-each>
	        </h2>
			</xsl:for-each>
        </h1>
		</xsl:for-each-group>
		 -->
	</xsl:template>
	
	<xsl:template match="text:h[@text:outline-level='1']">
      	<h1>
      		<xsl:attribute name="t">
      			<xsl:value-of select="text()"/>
      		</xsl:attribute>
      	</h1>
	</xsl:template>
	
	<xsl:template match="text:h[@text:outline-level='2']">
      	<h2>
      		<xsl:attribute name="t">
      			<xsl:value-of select="text()"/>
      		</xsl:attribute>
      	</h2>
	</xsl:template>
	
	<xsl:template match="text:h[@text:outline-level='3']">
      	<h3>
      		<xsl:attribute name="t">
      			<xsl:value-of select="text()"/>
      		</xsl:attribute>
      	</h3>
	</xsl:template>
	
	<xsl:template match="text:h[@text:outline-level='4']">
      	<h4>
      		<xsl:attribute name="t">
      			<xsl:value-of select="text()"/>
      		</xsl:attribute>
      	</h4>
	</xsl:template>
	
	<xsl:template match="text:tab">
		<xsl:text disable-output-escaping="yes"><![CDATA[&#160;]]></xsl:text>
	</xsl:template>

	<xsl:template match="text:a">
		<a>
			<xsl:attribute name="href"><xsl:value-of select="@xlink:href"/></xsl:attribute>
			<xsl:value-of select="text:span"/>
		</a>
	</xsl:template>

	<xsl:template match="text:p">
		<xsl:choose>
			<xsl:when test="count(draw:frame) &gt; 0">
				<xsl:apply-templates/>
			</xsl:when>
			<xsl:otherwise>
			<xsl:if test="string-length() &gt; 0">
				<p><xsl:apply-templates/></p>
			</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="text:p" mode="bare">
			<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="draw:frame">
		<img href="BPM.jpg" height="355px">
			<xsl:attribute name="href">
				<xsl:apply-templates/>
			</xsl:attribute>
			<xsl:value-of select="@draw:name"/>
		</img>
	</xsl:template>

	<xsl:template match="draw:image">
		<xsl:value-of select="@xlink:href"/>
	</xsl:template>

	<xsl:template match="text:list">
		<list>
			<xsl:apply-templates/>
		</list>
	</xsl:template>

	<xsl:template match="text:list-item">
		<le><xsl:apply-templates mode="bare"/></le>
	</xsl:template>

	<xsl:template match="table:table">
		<xsl:choose>
			<xsl:when test="count(table:table-column) &gt; 1">
				<table width="8cm">
					<xsl:apply-templates/>
				</table>
			</xsl:when>
			<xsl:otherwise>
				<code>
					<xsl:apply-templates mode="bare" select="table:table-row/table:table-cell"/>
				</code>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="table:table-column">
		<tw>3cm</tw>
	</xsl:template>

	<xsl:template match="table:table-row">
		<tr><xsl:apply-templates/></tr>
	</xsl:template>

	<xsl:template match="table:table-cell">
		<td><xsl:apply-templates mode="bare"/></td>
	</xsl:template>

</xsl:stylesheet>
